package net.vinote.smart.platform.service.action;

import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.List;

import net.vinote.smart.platform.comm.PlatformConst;
import net.vinote.smart.platform.comm.StringFunction;
import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.DBManagerFactory;
import net.vinote.smart.platform.module.database.DBTransaction;
import net.vinote.smart.platform.module.database.Field;
import net.vinote.smart.platform.module.database.Table;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.service.SRE;
import net.vinote.smart.platform.service.bean.DataWindowCatalog;
import net.vinote.smart.platform.service.util.AutoInjection;
import net.vinote.smart.platform.service.util.Permission;
import net.vinote.smart.platform.service.util.Permission.PermissionEnum;
import net.vinote.smart.platform.service.util.Permission.PermissionRelation;

/**
 * 显示模板Action类
 * 
 * @author 郑俊伟
 */
public class DataWindowAction {
	@AutoInjection
	private String doNo;

	@AutoInjection
	private String newDoNo;

	@AutoInjection
	private String fieldCode;

	@AutoInjection
	private String tableName;

	/**
	 * 删除一条dataobject_library记录
	 * 
	 * @return
	 * @throws SQLException
	 */
	@Permission(PermissionEnum.SMART_DW_LIBRARY_DELETE)
	public String deleteDWLibrary(DBTransaction tx) throws SQLException {
		DBManager bm = null;
		// -------第一步:删除dataobject_event记录----
		bm = DBManagerFactory.getInstance("dataobject_event");
		tx.join(bm);
		bm.deleteWhereSql("doNo=? and fieldCode=?", doNo, fieldCode);
		// -------第二步:删除dataobject_library记录----
		bm = DBManagerFactory.getInstance("dataobject_library");
		tx.join(bm);
		bm.deleteWhereSql("doNo=? and fieldcode=?", doNo, fieldCode);
		return PlatformConst.SUCCESS;
	}

	/**
	 * 复制一份显示模板
	 * 
	 * @return
	 * @throws SQLException
	 */
	@Permission(PermissionEnum.SMART_DW_CATALOG_UPDATE)
	public String copyDataWindow(DBTransaction tx) throws SQLException {
		DBManager bm = DBManagerFactory.getInstance("dataobject_catalog");
		tx.join(bm);
		BizObject bo = bm
				.createQuery(
						SRE.getAttribute(
								PlatformConst.SRE_PREFIX_TABLE
										+ "dataobject_catalog", Table.class)
								.getFieldsName(), "doNo=?").setParamVal(doNo)
				.getSingleResult();
		bo.setAttribute("doNO", doNo + "_copy");
		bm.saveBizObject(bo);
		bm = DBManagerFactory.getInstance("dataobject_library");
		tx.join(bm);
		List<BizObject> list = bm
				.createQuery(
						SRE.getAttribute(
								PlatformConst.SRE_PREFIX_TABLE
										+ "dataobject_library", Table.class)
								.getFieldsName(), "doNo=?").setParamVal(doNo)
				.getResultList();
		for (BizObject b : list) {
			b.setAttribute("doNo", doNo + "_copy");
			bm.saveBizObject(b);
		}
		return PlatformConst.SUCCESS;
	}

	/**
	 * 重命名显示模板
	 * 
	 * @return
	 * @throws SQLException
	 */
	@Permission(PermissionEnum.SMART_DW_CATALOG_UPDATE)
	public String reNameDataWindow(DBTransaction tx) throws SQLException {
		DBManager bm = DBManagerFactory.getInstance("dataobject_catalog");
		tx.join(bm);
		bm.update(new String[] { "doNo" }, "doNo=?", newDoNo, doNo);
		bm = DBManagerFactory.getInstance("dataobject_library");
		tx.join(bm);
		bm.update(new String[] { "doNo" }, "doNo=?", newDoNo, doNo);
		return PlatformConst.SUCCESS;
	}

	/**
	 * 自动创建指定表的显示模板
	 * 
	 * @return
	 * @throws SQLException
	 */
	@Permission(PermissionEnum.SMART_DW_CATALOG_INSERT)
	public String autoCreateDataWindow(DBTransaction tx) throws SQLException {
		List<Field> fileds = SRE.getTable(tableName).getFields();
		DBManager bm = DBManagerFactory.getInstance("dataobject_catalog");
		tx.join(bm);
		BizObject dcBo = new BizObject();
		dcBo.setAttribute("doNo", doNo);
		dcBo.setAttribute("mainTable", tableName);
		bm.saveBizObject(dcBo);
		bm = DBManagerFactory.getInstance("dataobject_library");
		DecimalFormat format = new DecimalFormat("0000");
		int i = 0;
		for (Field f : fileds) {
			BizObject dlBo = new BizObject();
			dlBo.setAttribute("doNo", doNo);
			dlBo.setAttribute("fieldCode", f.getFieldName());
			dlBo.setAttribute("sortNo", format.format(++i * 10));
			dlBo.setAttribute("dataObj", "O");
			dlBo.setAttribute("fieldName", f.getColRemark());
			dlBo.setAttribute("colField", f.getFieldName());
			dlBo.setAttribute("showTag", "input[text]");
			dlBo.setAttribute("isVisible", PlatformConst.YES);
			dlBo.setAttribute("isReadonly", PlatformConst.NO);
			dlBo.setAttribute("isInuse", PlatformConst.YES);
			bm.saveBizObject(dlBo);
		}
		return PlatformConst.SUCCESS;
	}

	/**
	 * 指定的doNO的显示模板是否已存在
	 * 
	 * @return
	 * @throws SQLException
	 */
	@Permission(PermissionEnum.SMART_DW_CATALOG_QUERY)
	public String isExistDoNo() throws SQLException {
		DBManager bm = DBManagerFactory.getInstance("dataobject_catalog");
		return bm.createQuery(new String[] { "1" }, "doNo=?").setParamVal(doNo)
				.getResultList().size() > 0 ? PlatformConst.SUCCESS
				: PlatformConst.FAIL;
	}

	/**
	 * 根据doNo获取对应主表的主键
	 * 
	 * @return
	 */
	@Permission(value = { PermissionEnum.SMART_DISABLE }, PermissionRelation = PermissionRelation.NOT)
	public String getTableKeysByDoNo() {
		DataWindowCatalog catalog = SRE.getDataWindowCatalog(doNo);
		StringBuffer keysSB = new StringBuffer();
		if (catalog != null) {
			String tableName = catalog.getMainTable();
			Table table = SRE.getTable(tableName);
			if (table != null) {
				List<Field> keyList = table.getKey();
				int size = keyList.size();
				if (size > 0) {
					keysSB.append(keyList.get(0).getFieldName());
					while (--size > 0) {
						keysSB.append(",").append(
								keyList.get(size).getFieldName());
					}
				}
			}
		}
		return keysSB.toString();
	}

	/**
	 * 获取当前显示模板对应的主表名称,若检索不到则返回空字符串
	 * 
	 * @return
	 */
	@Permission(PermissionEnum.SMART_DW_CATALOG_QUERY)
	public String getMainTableName() {
		DataWindowCatalog catalog = SRE.getDataWindowCatalog(doNo);
		if (catalog != null) {
			return StringFunction.nullToBlank(catalog.getMainTable());
		}
		return "";
	}
}
